const db = wx.cloud.database()
const questionList = db.collection('question')
let all = []
Page({
  /**
   * 页面的初始数据
   */
  data: {
    percent: 0, //进度条
    total: 0,
    isSelect: false, //是否选中
    sbject: [],
    userSelect: '', //记录用户最后选择的答案
    current: 1, //从第一题开始
    isParse: false,
    userIntegral: '',
    length: '',
    ltem: '',
    index: '', //题号，功能未实现
  },

  /**
   * 生命周期函数--监听页面加载
   */

  async onLoad() {
    //1.获取缓存里的openid
    let openId = wx.getStorageSync('openId')
    // 2.获取题目
    let list = await db.collection('question').where({
     unit:'第一章 绪论'
    }).get()
    all = all.concat(list.data)
    let subject = all[0]
    console.log(subject)
    this.setData({
      subject: subject,
      total: all.length,
      length: subject.options.length,
      ltem: subject.options
    })
    //3.获取用户积分
    db.collection('user').doc(openId).get()
      .then(res => {
        let userIntegral = res.data.userIntegral
        this.setData({
          userIntegral: userIntegral
        })
      })







    // let count = await db.collection('likework').count()
    // count = count.total
    // //2.通过for循环做多次请求，并把多次请求的数据放在一个数组里
    // for (let i = 0; i < count; i += 10) {      //自己设置每次获取数据的量
    //   let openid = this.data.userInfo.openId
    //   console.log(openid)
    //   let list = await db.collection('likework').where({_openid:openid}).skip(i).get()
    //   all = all.concat(list.data)
    // }
    // let subject = all[0]
    // this.setData({
    //   subject: subject,
    //   total: all.length,
    //   length: subject.options.length,
    //   ltem: subject.options
    // })

  },


  //用户选择
  radioChange(e) {
    let userSelect = e.detail.value
    this.setData({
      userSelect,
      isParse: true
    })
    if (this.data.isParse) {
      this.setData({
        Parse: this.data.subject.Parse
      })
    } else {
      this.setData({
        Parse: ""
      })
    }
    //3.判断用户是否答对

    var answer2 = this.data.subject.answer
    if (answer2 == userSelect) {
      var userIntegral = this.data.userIntegral += 5
      //以下是用户答对增加积分函数
      //获取缓存里的openid
      let openId = wx.getStorageSync('openId')
      db.collection("user").doc(openId).update({
        data: {
          userIntegral: userIntegral
        }
      }).then(res => {
        console.log(res)
      })

   
      wx.showToast({
        title: '正确',
        icon: 'success',
        duration: 500
      })

      // 答对就自动跳
      setTimeout(()=>{
       this.next()
    },1000)





    } else {
      wx.showToast({
        title: '错误',
        icon: 'error'
      })
      // 如果用户答错就减积分
      //获取缓存里的openid
      let openId = wx.getStorageSync('openId')
      var userIntegral = this.data.userIntegral -= 3
      db.collection("user").doc(openId).update({
        data: {
          userIntegral: userIntegral
        }
      }).then(res => {
        console.log(res)
      })


      //4.记录用户答错的题，方便用户查缺补漏
      var subjectNow = this.data.subject
      subjectNow.userSelect = userSelect
      console.log(subjectNow)
      var subjectNowid =  subjectNow._id
      // let openId = wx.getStorageSync('openId')
      db.collection('errorwork').where({
        _openid:openId,
        _id:subjectNowid
      }).get().then(res=>{
        let list = res.data
        if (list=='') {
          db.collection('errorwork') .add({
            data: {
              title: subjectNow.title,
              options: subjectNow.options,
              answer: subjectNow.answer,
              Parse: subjectNow.Parse,
              type: subjectNow.type,
              knowledge: subjectNow.knowledge,
              difficulty: subjectNow.difficulty,
              unit: subjectNow.unit
            }
        }
      )
        }
      })
      
     
        
  
   

    
      

      

    }
  },



  
  //下一题
  next() {
    //1.获取用户选项并判空
    let userSelect = this.data.userSelect
    if (!userSelect) {
      wx.showToast({
        title: '未作答',
        icon: 'none',
        duration: 500
      })
      return
    }

    //2.如果用户有选择，就更新进度条
    let num = this.data.current + 1
    let subject = all[num - 1]
    if (num > all.length) {
      wx.showToast({
        title: '已经是最后一题',
        icon: 'none'
      })
      return
    } else {
      this.setData({
        isSelect: false,
        subject,
        current: num,
        isParse: false,
        percent: (((num) / all.length) * 100).toFixed(0)
      })
    }
    this.data.userSelect = ''
  },

  //上一题
  back() {
    let num = this.data.current - 1
    if (num == 0) {
      wx.showToast({
        title: '已经是第一题',
        icon: 'none'
      })
      return
    }
    let subject = all[num - 1]
    this.setData({
      isSelect: false,
      subject,
      current: num,
      isParse: false,
      percent: (((num - 1) / all.length) * 100).toFixed(0)
    })
  },

  //用户收藏
  collect() {
    let userSelect = this.data.userSelect
    wx.showToast({
      title: '收藏成功',
      icon: 'success'
    })

    var subjectNow = this.data.subject
    var subjectNowid =  subjectNow._id
    // let openId = wx.getStorageSync('openId')
    db.collection('likework').where({
      _openid:openId,
      _id:subjectNowid
    }).get().then(res=>{
      let list = res.data
      if (list=='') {
        db.collection('likework') .add({
          data: {
            title: subjectNow.title,
            options: subjectNow.options,
            answer: subjectNow.answer,
            Parse: subjectNow.Parse,
            type: subjectNow.type,
            knowledge: subjectNow.knowledge,
            difficulty: subjectNow.difficulty,
            unit: subjectNow.unit
          }
      }
    )
      }
    })
    
   

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})